package com.s69.appback.controller;

import com.s69.appback.dto.LoginUserDto;
import com.s69.appback.service.DevUserService;
import com.s69.appback.util.SessionUtils;
import com.s69.appback.vo.LoginUserVo;
import com.s69.appback.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class LoginUserController {
    @Autowired
    DevUserService userService;
    @PostMapping("/login")
    public ResultVo login(@RequestBody LoginUserDto userDto){
        LoginUserVo vo=null;
        try {
            vo=userService.login(userDto);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
        return ResultVo.success("登录成功",vo);
    }
    @GetMapping("/logout")
    public ResultVo logout(HttpServletRequest request){
        String token = request.getHeader("token");
        SessionUtils.removeToken(token);
        return ResultVo.success("",null);
    }
}
